/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package struts;

import Service.*;
import java.io.BufferedReader;
import javabeans.GroupPlan;
import javabeans.Member;
import javabeans.Plan;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 *
 * @author zhi
 */
public class SpaceAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Cache-Control", "no-store"); //HTTP1.1
        response.setHeader("Pragma", "no-cache"); //HTTP1.0
        response.setDateHeader("Expires", 0); //prevents catching at proxy server

        String action = request.getParameter("action");

        if("newPersonalPlan".equals(action)){
System.out.println("new persoanl plan");
            String userid = request.getParameter("userid");
            String name = request.getParameter("name");
            String description = request.getParameter("description");
System.out.println(userid);
System.out.println(name);
System.out.println(description);

            boolean newPersonalPlanSuccess = true;

            PlanService ps = new PlanServiceImpl();
            Plan plan = ps.newPlan(name, description, userid);
            //String planid = "101";
            int planid = plan.getPlanid();
System.out.println(planid);

            response.setContentType("text/json");
            if(newPersonalPlanSuccess == true) {
                response.getWriter().write("{ success: true, planid:'"+planid+"'}");
            }
            else {
                response.getWriter().write("{ success: false, errors:{info:'error'} }");
            }
        }else if("newGroupPlan".equals(action)){
System.out.println("new group plan");
            String userid = request.getParameter("userid");
            String name = request.getParameter("name");
            String description = request.getParameter("description");
System.out.println(userid);
System.out.println(name);
System.out.println(description);

            boolean newGroupPlanSuccess = true;

            MemberService ms = new MemberServiceImpl();
            GroupPlan groupplan = ms.createGroupPlan(name, description, userid);
            //String planid = "101";
            int groupplanid = groupplan.getGroupplanid();
System.out.println(groupplanid);

            response.setContentType("text/json");
            if(newGroupPlanSuccess == true) {
                response.getWriter().write("{ success: true, groupplanid:'"+groupplanid+"'}");
            }
            else {
                response.getWriter().write("{ success: false, errors:{info:'error'} }");
            }
        } else if("joinGroupPlan".equals(action)){
System.out.println("join group plan");
            String email = request.getParameter("userid");
            String groupplanidString = request.getParameter("groupplanid");
System.out.println("userid: " + email);
System.out.println("groupplanid: " + groupplanidString);
            int groupplanid = Integer.parseInt(groupplanidString);

            MemberService ms = new MemberServiceImpl();
            Member member = ms.getMember(email);
            GroupPlanService gps = new GroupPlanServiceImpl();
            GroupPlan groupplan = gps.getGroupPlan(groupplanid);
            
            boolean joinGroupPlanSuccess = ms.joinGroup(member, groupplan);

            response.setContentType("text/json");
            if(joinGroupPlanSuccess == true) {
                response.getWriter().write("{ success: true}");
            }
            else {
                response.getWriter().write("{ success: false, errors:{info:'error'} }");
            }
        } else if("leaveGroupPlan".equals(action)){
System.out.println("leave group plan");
            String email = request.getParameter("userid");
            String groupplanidString = request.getParameter("groupplanid");
System.out.println("userid: " + email);
System.out.println("groupplanid: " + groupplanidString);
            int groupplanid = Integer.parseInt(groupplanidString);

            MemberService ms = new MemberServiceImpl();
            Member member = ms.getMember(email);
            GroupPlanService gps = new GroupPlanServiceImpl();
            GroupPlan groupplan = gps.getGroupPlan(groupplanid);

            if(member==null) System.out.println("member: null!");
            if(groupplan==null) System.out.println("groupplan: null!");
            System.out.println(member.getId());
            System.out.println(groupplan.getGroupplanid());

            boolean leaveGroupPlanSuccess = ms.leaveGroup(member, groupplan);

            response.setContentType("text/json");
            if(leaveGroupPlanSuccess == true) {
                response.getWriter().write("{ success: true}");
            }
            else {
                response.getWriter().write("{ success: false, errors:{info:'error'} }");
            }
        }
        return super.execute(mapping, form, request, response);
    }

}
